﻿@{
    ViewBag.Title = "用户信息";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">组织机构</h3>
                <div class="box-tools pull-right">
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm">
                        <i class="fa fa-refresh">刷新</i>
                    </a>
                    <a class="btn btn-danger  btn-sm" data-widget="collapse" data-toggle="tooltip" title="伸缩" style="margin-right: 5px;">
                        <i class="fa fa-minus"></i>
                    </a>
                </div>
            </div>
            <div class="box-body" id="tree-box-body" style="overflow: auto">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">人员列表</h3>
                <div class="box-tools">
                    <div class="input-group" style="width: 250px;">
                        <input type="text" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                        <div class="input-group-btn">
                            <a class="btn btn-sm btn-default">
                                <i class="fa fa-search"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-body no-padding">
                <div class="mailbox-controls">
                    <div class="btn-group" id="partial_button_toolbar">
                        @{ Html.RenderPartial("~/Areas/Common/Views/Shared/_PartialButton.cshtml"); }
                    </div>
                    <a data-toggle="tooltip" title="刷新" class="btn btn-default btn-sm">
                        <i class="fa fa-refresh">刷新</i>
                    </a>
                </div>
                <div class="mailbox-jqgrid">
                    <table id="list"></table>
                    <div id="pager"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {
        height = UtilWindowHeightWidth().WinH - 218;
        $("#tree-box-body").height(height);
        initGird();
        initTree();
        initTreeData();
    });

    var zNodes,
        treeObj,
        setting,
        $grid,
        height;

    //初始化表格
    function initGird() {
        $grid = $("#list").jgridview(
        {
            loadonce: false,
            multiselect: false,
            url: "/System/User/GetPagingUser",
            colModel: [{ label: "", name: "UserId", align: "", width: "", fixed: false, hidden: true }, { label: "", name: "OrganizationId", align: "", width: "", fixed: false, hidden: true }, { label: "登录名", name: "Code", index: "u.Code", align: "", width: "120", fixed: true, hidden: false }, { label: "姓名", name: "Name", index: "u.Name", align: "", width: "60", fixed: true, hidden: false }, { label: "归属组织", name: "OrganizationName", index: "OrganizationId", align: "", width: "150", fixed: true, hidden: false }, { label: "电话号码", name: "Mobile", align: "", width: "100", fixed: true, hidden: false }, { label: "冻结", name: "IsFreeze", index: "u.IsFreeze", align: "center", width: "50", fixed: true, hidden: false, formatter: "YesOrNo" }, { label: "首次登录时间", name: "FirstVisitTime", align: "", width: "120", fixed: true, hidden: false }, { label: "最后登录时间", name: "LastVisitTime", align: "", width: "120", fixed: true, hidden: false }, { label: "创建时间", name: "CreatTime", index: "u.CreateTime", align: "", width: "120", fixed: true, hidden: false }, { label: "创建人员", name: "CreateUserName", index: "u.CreateUserName", align: "", width: "60", fixed: false, hidden: false }, { label: "最后修改时间", name: "UpdateTime", index: "u.UpdateTime", align: "", width: "120", fixed: false, hidden: false }, { label: "最后修改人员", name: "UpdateUserName", index: "u.UpdateUserName", align: "", width: "60", fixed: false, hidden: false }, { label: "备注", name: "Remark", align: "", width: "200", fixed: true, hidden: false }],
            height: height - 100,
            sortname: "u.CreateTime",
            rowList: [200, 500, 1000]
            //,
            //onSelectRow: function (rowid) {
            //    getById(rowid);
            //},
            //loadComplete: function () {
            //    //选中第一条
            //    GridSetSelection($grid, 1);
            //    getById(1);
            //}
        });
    }

    //获取详细信息
    function getById(rowid) {
        var rowDatas = $grid.jqGrid("getRowData", rowid);
        if (typeof (rowDatas["UserId"]) != "undefined") {
            UtilAjaxPost("/System/User/GetDetailByUserId", { id: rowDatas["UserId"] },
                function(val) {
                    $(".tableView").find("label").each(function() {
                        var $this = $(this), id = $this.attr("id");
                        (val[id] && typeof (val[id]) === "string") && $this.text(val[id]);
                    });
                    //角色
                    var roleHtml = template("role-template", val);
                    $("#tabs-role").html(roleHtml);

                    //岗位
                    var postHtml = template("post-template", val);
                    $("#tabs-post").html(postHtml);

                    //组
                    var groupHtml = template("group-template", val);
                    $("#tabs-group").html(groupHtml);
                }
            );
        } else {
            $(".tableView label").html("");
            var val = [];
            //角色
            var roleHtml = template("role-template", val);
            $("#tabs-role").html(roleHtml);

            //岗位
            var postHtml = template("post-template", val);
            $("#tabs-post").html(postHtml);

            //组
            var groupHtml = template("group-template", val);
            $("#tabs-group").html(groupHtml);
        }
    }

    //获取表格数据
    function getGridData() {
        //重新查询
        $grid.trigger("reloadGrid", [{ page: 1 }]);
    }

    //导出Excel
    function reportExcel() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        var organizationId;
        if (treeNode.length === 0) {
            organizationId = Language.common.guidempty;
        } else {
            organizationId = treeNode[0].id;
        }
        var isFreeze = $("#SearchIsFreeze").val();
        var $form = $("<form method='post' action='/System/User/ExportExcel?" +
            "PrivilegeMasterValue=" + organizationId + "&CreateTime=" + $("#u.CreateTime").val() + "&Code=" + $("#u.Code").val() + "&Name=" + $("#u.Name").val() + "&IsFreeze=" + (isFreeze == "" ? null : (isFreeze == "0" ? false : true)) +
            "' enctype='multipart/form-data'></form>");
        $form.appendTo($("body")).submit().remove();
        // UtilAjaxPostWaitHiddenOverlay(
        //    "/System/User/ExportExcel",
        //    {
        //        PrivilegeMasterValue: organizationId,
        //        CreateTime: $("#u.CreateTime").val(),
        //        Code: $("#u.Code").val(),
        //        Name: $("#u.Name").val(),
        //        IsFreeze: isFreeze == "" ? null : (isFreeze == "0" ? false : true)
        //    },
        //    function (data) {

        //    }
        //);
    }

    //操作:新增
    function add() {
        var treeNode = ZtreeGetSelectedNodes($.fn.zTree.getZTreeObj("tree"));
        if (treeNode.length === 0) {
            DialogTipsMsgWarn("请选择需要添加人员的组织机构", 2000);
            return;
        }
        ArtDialogOpen("/System/User/Edit?orgId=" + treeNode[0].id, "新增用户信息", 580);
    }

    //操作:编辑
    function edit() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/User/Edit?userId=" + info.UserId + "&orgId=" + info.OrganizationId, "修改用户信息-" + info.Name, 580);
        });
    }

    //删除匹配项
    function del() {
        //查看是否选中
        GridIsSelect($grid, function() {
            ArtDialogConfirm(Language.common.deletemsg, function() {
                UtilAjaxPostWait(
                    "/System/User/DeleteUser",
                    { id: GridGetSingSelectData($grid).UserId },
                    perateStatus
                );
            });
        });
    }

    //初始化组织机构
    function initTree() {
        //配置
        setting = {
            view: {
                dblClickExpand: false,
                showLine: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            expandSpeed: "",
            callback: {
                onClick: onClickTree
            }
        };
    }

    //初始化树结构:同步
    function initTreeData() {
        UtilAjaxPostAsync("/System/Organization/GetOrganizationTree", null, function(data) {
            zNodes = data;
            treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
            treeObj.expandAll(true);
        });
    }

    //树点击触发
    function onClickTree(e, treeId, treeNode) {
        resetOrgId(treeNode.id);
        getGridData();
    }

    //重置组织机构
    function resetOrgId(id) {
        $("#PrivilegeMasterValue").val(id);
    }

    //请求完成
    function perateStatus(data) {
        DialogAjaxResult(data);
        if (data.ResultSign === 0) {
            getGridData();
        }
    }

    //加载页面数据
    function reloadTreeAndGrid() {
        resetOrgId(null);
        initTreeData();
        getGridData();
    }

    //模块权限
    function menuPermission() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Permission/Menu?privilegeMasterValue=" + info.UserId + "&privilegeMaster=" + Language.privilegeMaster.user, "模块权限授权-" + info.Name, 380);
        });
    }

    //模块按钮权限
    function functionPermission() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Permission/Function?privilegeMasterValue=" + info.UserId + "&privilegeMaster=" + Language.privilegeMaster.user, "模块按钮权限授权-" + info.Name, 810);
        });
    }

    //字段权限
    function fieldPermission() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Permission/Field?privilegeMasterValue=" + info.UserId + "&privilegeMaster=" + Language.privilegeMaster.user, "字段权限授权-" + info.Name, 810);
        });
    }

    //数据权限
    function dataPermission() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Permission/Data?privilegeMasterValue=" + info.UserId + "&privilegeMaster=" + Language.privilegeMaster.user, "数据权限授权-" + info.Name, 810);
        });
    }

    //用户角色
    function userRole() {
        //查看是否选中
        GridIsSelect($grid, function() {
            var info = GridGetSingSelectData($grid);
            ArtDialogOpen("/System/Role/Chosen?userId=" + info.UserId, "维护用户角色-" + info.Name, true, 450, 870);
        });
    }

    //重置密码
    function resetPassword() {
        //查看是否选中
        GridIsSelect($grid, function() {
            ArtDialogConfirm(Language.system.resetPassword, function() {
                UtilAjaxPostWait(
                    "/System/User/ResetPassword",
                    { id: GridGetSingSelectData($grid).UserId },
                    perateStatus
                );
            });
        });
    }

</script>